package com.simon.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simon.train.business.domain.DailyTrainCarriage;
import com.simon.train.business.domain.DailyTrainCarriageExample;
import com.simon.train.business.domain.TrainCarriage;
import com.simon.train.business.dto.DailyTrainCarriageQueryDto;
import com.simon.train.business.dto.DailyTrainCarriageSaveDto;
import com.simon.train.business.enums.SeatColEnum;
import com.simon.train.business.mapper.DailyTrainCarriageMapper;
import com.simon.train.business.vo.DailyTrainCarriageQueryVO;
import com.simon.train.common.util.SnowUtil;
import com.simon.train.common.vo.PageVO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainCarriageService {

    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainCarriageService.class);

    @Resource
    private DailyTrainCarriageMapper dailyTrainCarriageMapper;

    @Resource
    private TrainCarriageService trainCarriageService;

    /**
     * 根据日期，列车编号和座位类型选择车厢并返回
     * @param date
     * @param trainCode
     * @param seatType
     * @return
     */
    public List<DailyTrainCarriage> selectBySeatType(Date date, String trainCode, String seatType){
        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        dailyTrainCarriageExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andSeatTypeEqualTo(seatType);
        List<DailyTrainCarriage> carriages = dailyTrainCarriageMapper.selectByExample(dailyTrainCarriageExample);
        return carriages;
    }

    public void SaveDailyTrainCarriage(DailyTrainCarriageSaveDto dailyTrainCarriageSaveDto){
        /**
         * 自动计算列数和座位总数
         * 不需要前端去计算 前端只需要传入行数即可
         */
        List<SeatColEnum> colsByType = SeatColEnum.getColsByType(dailyTrainCarriageSaveDto.getSeatType());
        dailyTrainCarriageSaveDto.setColCount(colsByType.size());
        dailyTrainCarriageSaveDto.setSeatCount(dailyTrainCarriageSaveDto.getColCount() * dailyTrainCarriageSaveDto.getRowCount());

        DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(dailyTrainCarriageSaveDto, DailyTrainCarriage.class);
        if (ObjectUtil.isNull(dailyTrainCarriage.getId())){
            //对于首次保存 也就是新增 需要对字段单独做处理
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            DateTime time = DateTime.now();
            dailyTrainCarriage.setCreateTime(time);
            dailyTrainCarriage.setUpdateTime(time);
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        } else {
            dailyTrainCarriage.setUpdateTime(DateTime.now());
            dailyTrainCarriageMapper.updateByPrimaryKey(dailyTrainCarriage);
        }

    }

    public PageVO<DailyTrainCarriageQueryVO> QueryDailyTrainCarriageList(DailyTrainCarriageQueryDto dailyTrainCarriageQueryDto){
        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        dailyTrainCarriageExample.setOrderByClause("date desc, train_code asc, `index` asc");
        DailyTrainCarriageExample.Criteria criteria = dailyTrainCarriageExample.createCriteria();

        if(ObjectUtil.isNotEmpty(dailyTrainCarriageQueryDto.getCode())){
            criteria.andTrainCodeEqualTo(dailyTrainCarriageQueryDto.getCode());
        }
        if(ObjectUtil.isNotNull(dailyTrainCarriageQueryDto.getDate())){
            criteria.andDateEqualTo(dailyTrainCarriageQueryDto.getDate());
        }

        LOG.info("当前请求的页码：{}", dailyTrainCarriageQueryDto.getPage());
        LOG.info("每页条数：{}", dailyTrainCarriageQueryDto.getPageSize());

        PageHelper.startPage(dailyTrainCarriageQueryDto.getPage(),dailyTrainCarriageQueryDto.getPageSize());
        List<DailyTrainCarriage> dailyTrainCarriageList = dailyTrainCarriageMapper.selectByExample(dailyTrainCarriageExample);

        PageInfo<DailyTrainCarriage> pageInfo = new PageInfo<>(dailyTrainCarriageList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<DailyTrainCarriageQueryVO> lists = BeanUtil.copyToList(dailyTrainCarriageList, DailyTrainCarriageQueryVO.class);
        PageVO<DailyTrainCarriageQueryVO> pageVO = new PageVO<>();
        pageVO.setList(lists);
        pageVO.setTotal(pageInfo.getTotal());

        return pageVO;
    }

    public void deleteDailyTrainCarriage(Long id) {
        dailyTrainCarriageMapper.deleteByPrimaryKey(id);
    }

    /**
     * 生成date日期的trainCode的车次车厢的数据
     * @param trainCode
     * @param date
     */
    public void GenerateDailyTrainCarriage(String trainCode, Date date){
        LOG.info("生成日期【{}】的车次【{}】车厢信息开始", DateUtil.formatDate(date),trainCode);

        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        dailyTrainCarriageExample.createCriteria()
                .andTrainCodeEqualTo(trainCode)
                .andDateEqualTo(date);
        dailyTrainCarriageMapper.deleteByExample(dailyTrainCarriageExample);

        List<TrainCarriage> trainCarriages = trainCarriageService.SelectByTrainCode(trainCode);

        if(CollUtil.isEmpty(trainCarriages)){
            LOG.info("改车次没有车厢信息，生成结束");
            LOG.info("生成日期【{}】的车次【{}】车厢信息结束", DateUtil.formatDate(date),trainCode);
            return;
        }

        for (TrainCarriage trainCarriage : trainCarriages) {
            DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(trainCarriage, DailyTrainCarriage.class);
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            DateTime time = DateTime.now();
            dailyTrainCarriage.setCreateTime(time);
            dailyTrainCarriage.setUpdateTime(time);
            dailyTrainCarriage.setDate(date);
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        }

        LOG.info("生成日期【{}】的车次【{}】车厢信息结束", DateUtil.formatDate(date),trainCode);
    }
}
